package com.lagou.task12;

public class StringToByteCharTest {
    public static void main(String[] args) {
        //创建一个字符串
        String s1 = new String("liyulin");
        System.out.println("s1 =  " + s1);
        System.out.println("----------------------------------");
        //字符串转byte数组
        byte[] barr = s1.getBytes();
        for (int i = 0; i < barr.length; i++){
            System.out.println("第"+i+"元素："+barr[i]);
        }
        System.out.println("----------------------------------");
        //字符串转char数组
        char[] carr = s1.toCharArray();
        for (int i = 0; i < carr.length; i++){
            System.out.println("第"+i+"元素："+barr[i]);
        }
        System.out.println("----------------------------------");
        //字符数组和字节数组转字符串
        String s2 = new String(barr,2,2);
        String s3 = new String(carr,4,3);
        System.out.println("s2 = " + s2);
        System.out.println("s3 = " + s3);

        //获取字符串的长度
        String st1 = "abcdefg";
        System.out.println("数组长度"+st1.length());
        System.out.println("返回下标为4的字符"+st1.charAt(4));

        System.out.println("----------------------------------");
        //字符串为空
        System.out.println(st1.length() == 0);
        System.out.println(st1.isEmpty());

        //字符串转换为int
        String str2 = new String("1234");

//        1.
        int ib = Integer.parseInt(str2);
//        2.使用ascII码转换
        int sum = 0;
        int ia = 0;
        for (int i = 0; i < str2.length(); i++){
            ia = str2.charAt(i) - '0';
            sum = sum*10 + ia;
        }
        System.out.println("sum = " + sum);

        System.out.println("----------------------------------");
        //判断字符串是否是回文
        String sss = new String("上海自l来水来自海上");

        char[] csarr = new char[sss.length()];
        for (int i = 0; i < sss.length(); i++){
            csarr[i] = sss.charAt(sss.length()-i-1);
        }
        String sss2 = new String(csarr);
        System.out.println(sss.compareTo(sss2));

        System.out.println("----------------------------------");
        //compareTo比较大小规则，1.sts.chatAt(i)-sts1.charAt(i);大于0则sts大，小于0则sts1大，等于0则相等
        //如果在1的条件下发现长度不一样，则sts.length()-sts1.length()
//        sss.compareToIgnoreCase()和compareTo一样只不过他忽略大小写，统一按小写处理
        String sts = new String("Hello");
        String sts1 = new String("hello");
        String sts2 = new String("hello");
        String sts3 = new String("hello00");

    }
}
